package com.atguigu.chapter02;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * TODO 有界流 Wordcount： 文件
 *
 * @author cjp
 * @version 1.0
 * @date 2021/3/2 10:01
 */
public class Flink04_WC_BoundedStream_Lambda {
    public static void main(String[] args) throws Exception {
        // 1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 2.读取数据
        DataStreamSource<String> inputDS = env.readTextFile("input/word.txt");

        // 3.处理数据
        // 3.1 压平：切分、转换二元组（word，1）
        SingleOutputStreamOperator<Tuple2<String, Long>> wordAndOneDS = inputDS
                .flatMap((FlatMapFunction<String, Tuple2<String, Long>>) (value, out) -> {
                    // 切分
                    String[] words = value.split(" ");
                    for (String word : words) {
                        out.collect(Tuple2.of(word, 1L));
                    }
                })
                .returns(Types.TUPLE(Types.STRING, Types.LONG));// lambda的时候，泛型的类型擦除问题

        // 3.2 分组：按照 word分组
        KeyedStream<Tuple2<String, Long>, Tuple> wordAndOneKS = wordAndOneDS.keyBy(0);
        // 3.3 组内求和
        SingleOutputStreamOperator<Tuple2<String, Long>> resultDS = wordAndOneKS.sum(1);

        // 4.输出
        resultDS.print();

        // 5.启动
        env.execute();

    }


}
